package top.hzy520.Controller;

import top.hzy520.entity.User;
import top.hzy520.service.loginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class LoginServlet extends HttpServlet {
    loginServiceImpl loginService=new loginServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     String username=req.getParameter("username");
     String password=req.getParameter("password");
try {
  User user=loginService.login(username,password);
    if (user==null){
      req.getRequestDispatcher("/login.jsp").forward(req,resp);
    }else {
        req.getSession().setAttribute("user",user);
        req.getRequestDispatcher("/home.jsp").forward(req,resp);
    }
}catch (Exception e){

}
    }
}
